package com.longrise.android.byjk.plugins.vip;

import android.content.Context;
import android.widget.Toast;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.utils.DZZWTools;
import com.longrise.common.utils.PrintLog;

/**
 * Created by callxkj on 2017/1/11.
 * VIP管理类
 */

public class VipManager {

    private final static String TAG = "VipManager";

    public static void checkVip(final Context context, final CheckResultListener listener){
        String userid = UserInfor.getInstance().getUserid();
        String usersfzh = UserInfor.getInstance().getUsersfzh();
        EntityBean bean  = new EntityBean();
        bean.set("userid", userid);
        bean.set("cardno",usersfzh);
        EntityBean fbean = new EntityBean();
        fbean.set("bean",bean);
        DZZWTools.showWaitDialog(context);
        LoadDataManager.getInstance().callService("", UrlConstants.BaseUrl,
                "bbt_getVipUserInfo", fbean, new LoadDataManager.OnRequestCallListener() {
            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    if (result != null) {
                        EntityBean bean = (EntityBean) result;
                        String resultdesc = (String) bean.get("resultdesc");
                        int resultstate = (Integer) bean.get("resultstate");
                        if (resultstate == 1) {

                            UserInfor userInfor = UserInfor.getInstance();
                            EntityBean resultBean = bean.getBean("result");
                            //会员类型
                            Integer viptype = resultBean.getInt("viptype");
                            PrintLog.e(TAG, "viptype: " + viptype);
                            userInfor.setViptype(viptype);
                            //会员类型名称
//                            String viptypename = resultBean.getString("viptypename");
//                            userInfor.setViptypename(viptypename);
                            //截止日期
                            String deadline = resultBean.getString("deadline");
                            String[] split = deadline.split(" ");
                            userInfor.setDeadline(split[0].replace("-", "/"));
                            //剩余天数
                            Integer remainingdays = resultBean.getInt("remainingdays");
                            userInfor.setRemainingdays(remainingdays);

                            listener.onSuccess();
                        } else {
                            DZZWTools.showToast(context, resultdesc, Toast.LENGTH_SHORT);
                        }
                    }
                } catch (Exception e) {
                    DZZWTools.showToast(context, "解析查询vip返回数据失败", Toast.LENGTH_SHORT);
                }
            }

            @Override
            public void onError(String key, String service, Throwable ex, boolean isOnCallback) {
                DZZWTools.showToast(context, context.getString(R.string.nonetwork), 0);
            }

            @Override
            public void onFinished(String key, String service) {
                DZZWTools.dismissDialog(null);
            }
        });
    }

    public interface  CheckResultListener{
        void onSuccess();
    }
}
